﻿using UnityEngine;
using System.Collections.Generic;
using ProtoBuf.Message;

namespace Room
{
    public class NetUnionFightAnnounce : NetUnionFightBase
    {
        #region 数据
        bool isShowing;
        Queue<string> msg_queue = new Queue<string>();
        GameObject label;
        #endregion

        public NetUnionFightAnnounce(RoomUnionFight uf, RoomCharactorManager mgr, RoomNetUnionFight net) : base(uf, mgr, net)
        {
        }

        public void ReceiveMsg(string text)
        {
            
            //获取己方阵营，根据阵营选择是否播送通知
            var unionActor = charactorManager.GetSelfCharactor().ai as RCAIUnionActor;
            int selfCamp = unionActor.Camp;

            msg_queue.Enqueue(text);
            CheckShowAnnouce();

        }

        void CheckShowAnnouce()
        {
            if (msg_queue.Count <= 0 || isShowing)
                return;
            string text = msg_queue.Peek();

            if(label == null)
            {
                label = CreateScrLabel();
            }
            var script = label.GetComponent<UI.ScrollTextAni>();
            if (script)
            {
                script.StartMove(text);
                script.m_callback = OnMsgShowEnd;
            }
            isShowing = true;
        }

        void OnMsgShowEnd()
        {
            isShowing = false;

            if (msg_queue.Count > 0)
            {
                msg_queue.Dequeue();
            }
            CheckShowAnnouce();
            if (msg_queue.Count <= 0)
                GameObject.Destroy(label);
        }

        GameObject CreateScrLabel()
        {
            GameObject labelObj = GameObject.Instantiate(Resources.Load("prefab/ScrollText")) as GameObject;
            labelObj.transform.SetParent( GetUI().gameObject.transform);         
            labelObj.transform.localScale = Vector3.one;
            return labelObj;
        }

        public override void Tick()
        {
            base.Tick();

        }

        void PlayByType(int type)
        {

        }
    }

}
